/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.SystemActionOnComponent;
import it.polimi.games.epkb.entityClasses.SystemActionOnComponentPK;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.Component;
import it.polimi.games.epkb.entityClasses.SystemAction;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class SystemActionOnComponentJpaController implements Serializable {

    public SystemActionOnComponentJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }

    public SystemActionOnComponentJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(SystemActionOnComponent systemActionOnComponent) throws PreexistingEntityException, Exception {
        if (systemActionOnComponent.getSystemActionOnComponentPK() == null) {
            systemActionOnComponent.setSystemActionOnComponentPK(new SystemActionOnComponentPK());
        }
        systemActionOnComponent.getSystemActionOnComponentPK().setIdSystemAction(systemActionOnComponent.getSystemAction().getId());
        systemActionOnComponent.getSystemActionOnComponentPK().setIdComponent(systemActionOnComponent.getComponent().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Component component = systemActionOnComponent.getComponent();
            if (component != null) {
                component = em.getReference(component.getClass(), component.getId());
                systemActionOnComponent.setComponent(component);
            }
            SystemAction systemAction = systemActionOnComponent.getSystemAction();
            if (systemAction != null) {
                systemAction = em.getReference(systemAction.getClass(), systemAction.getId());
                systemActionOnComponent.setSystemAction(systemAction);
            }
            em.persist(systemActionOnComponent);
            if (component != null) {
                component.getSystemActionOnComponentCollection().add(systemActionOnComponent);
                component = em.merge(component);
            }
            if (systemAction != null) {
                systemAction.getSystemActionOnComponentCollection().add(systemActionOnComponent);
                systemAction = em.merge(systemAction);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSystemActionOnComponent(systemActionOnComponent.getSystemActionOnComponentPK()) != null) {
                throw new PreexistingEntityException("SystemActionOnComponent " + systemActionOnComponent + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(SystemActionOnComponent systemActionOnComponent) throws NonexistentEntityException, Exception {
        systemActionOnComponent.getSystemActionOnComponentPK().setIdSystemAction(systemActionOnComponent.getSystemAction().getId());
        systemActionOnComponent.getSystemActionOnComponentPK().setIdComponent(systemActionOnComponent.getComponent().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SystemActionOnComponent persistentSystemActionOnComponent = em.find(SystemActionOnComponent.class, systemActionOnComponent.getSystemActionOnComponentPK());
            Component componentOld = persistentSystemActionOnComponent.getComponent();
            Component componentNew = systemActionOnComponent.getComponent();
            SystemAction systemActionOld = persistentSystemActionOnComponent.getSystemAction();
            SystemAction systemActionNew = systemActionOnComponent.getSystemAction();
            if (componentNew != null) {
                componentNew = em.getReference(componentNew.getClass(), componentNew.getId());
                systemActionOnComponent.setComponent(componentNew);
            }
            if (systemActionNew != null) {
                systemActionNew = em.getReference(systemActionNew.getClass(), systemActionNew.getId());
                systemActionOnComponent.setSystemAction(systemActionNew);
            }
            systemActionOnComponent = em.merge(systemActionOnComponent);
            if (componentOld != null && !componentOld.equals(componentNew)) {
                componentOld.getSystemActionOnComponentCollection().remove(systemActionOnComponent);
                componentOld = em.merge(componentOld);
            }
            if (componentNew != null && !componentNew.equals(componentOld)) {
                componentNew.getSystemActionOnComponentCollection().add(systemActionOnComponent);
                componentNew = em.merge(componentNew);
            }
            if (systemActionOld != null && !systemActionOld.equals(systemActionNew)) {
                systemActionOld.getSystemActionOnComponentCollection().remove(systemActionOnComponent);
                systemActionOld = em.merge(systemActionOld);
            }
            if (systemActionNew != null && !systemActionNew.equals(systemActionOld)) {
                systemActionNew.getSystemActionOnComponentCollection().add(systemActionOnComponent);
                systemActionNew = em.merge(systemActionNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                SystemActionOnComponentPK id = systemActionOnComponent.getSystemActionOnComponentPK();
                if (findSystemActionOnComponent(id) == null) {
                    throw new NonexistentEntityException("The systemActionOnComponent with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(SystemActionOnComponentPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SystemActionOnComponent systemActionOnComponent;
            try {
                systemActionOnComponent = em.getReference(SystemActionOnComponent.class, id);
                systemActionOnComponent.getSystemActionOnComponentPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The systemActionOnComponent with id " + id + " no longer exists.", enfe);
            }
            Component component = systemActionOnComponent.getComponent();
            if (component != null) {
                component.getSystemActionOnComponentCollection().remove(systemActionOnComponent);
                component = em.merge(component);
            }
            SystemAction systemAction = systemActionOnComponent.getSystemAction();
            if (systemAction != null) {
                systemAction.getSystemActionOnComponentCollection().remove(systemActionOnComponent);
                systemAction = em.merge(systemAction);
            }
            em.remove(systemActionOnComponent);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<SystemActionOnComponent> findSystemActionOnComponentEntities() {
        return findSystemActionOnComponentEntities(true, -1, -1);
    }

    public List<SystemActionOnComponent> findSystemActionOnComponentEntities(int maxResults, int firstResult) {
        return findSystemActionOnComponentEntities(false, maxResults, firstResult);
    }

    private List<SystemActionOnComponent> findSystemActionOnComponentEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(SystemActionOnComponent.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public SystemActionOnComponent findSystemActionOnComponent(SystemActionOnComponentPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(SystemActionOnComponent.class, id);
        } finally {
            em.close();
        }
    }

    public int getSystemActionOnComponentCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<SystemActionOnComponent> rt = cq.from(SystemActionOnComponent.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
